package com.andy.eqm.server.client;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.andy.eqm.server.dto.ClientMsgWriter;
import com.andy.eqm.server.socket.GmAction;
import com.andy.eqm.server.socket.codec.DataWrap;

import io.netty.buffer.ByteBuf;
import io.netty.channel.ChannelHandlerContext;
import io.netty.channel.SimpleChannelInboundHandler;

public class FishClientHandler extends SimpleChannelInboundHandler<ByteBuf> {
	
	private Logger logger = LoggerFactory.getLogger(getClass());
	
	@Override
	public void channelActive(ChannelHandlerContext ctx) throws Exception {
		super.channelActive(ctx);
		// 执行登录
		ctx.writeAndFlush(new ClientMsgWriter(GmAction.ACTION_100,ClientRobot.userName));
	}

	@Override
	protected void channelRead0(ChannelHandlerContext ctx, ByteBuf msg) throws Exception {
		int action = msg.readInt();
		if(logger.isInfoEnabled()){
			logger.info("收到服务端消息:{}", action);
		}
		try {
			switch (action) {
			case GmAction.ACTION_100:
				ClientRobot.action100(ctx,new DataWrap(msg));
				break;
			case GmAction.ACTION_110:
				ClientRobot.action110(ctx,new DataWrap(msg));
				break;

			default:
				break;
			}
			
		} catch (Exception e) {
			logger.error("执行逻辑异常", e);
		}
	}

	@Override
	public void handlerRemoved(ChannelHandlerContext ctx) throws Exception {
		logger.info("Current channel handlerRemoved");
	}
	
	
	@Override
	public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) throws Exception {
		logger.error("连接异常", cause);
	}
	
}
